package com.activiti.z_six.controller;

import com.activiti.z_six.entity.formComponents.FormEntity;
import com.activiti.z_six.entity.formComponents.FormSortEntity;
import com.activiti.z_six.entity.taskAssignee.GenerWork;
import com.activiti.z_six.service.FormMapService;
import com.activiti.z_six.util.AjaxResponse;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
public class FormMapController {
    @Autowired
    private FormMapService formMapService;

    /**
     * 表单列表
     * @param response
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/form/formList")
    public AjaxResponse formList(HttpServletResponse response,
                                 @RequestParam("page") Integer page,
                                 @RequestParam("pagesize") Integer pagesize){
        HashMap<String,Object> formList=formMapService.formList(page,pagesize);
        return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                SystemConfig.ResponseCode.SUCCESS.getDesc(), formList);
    }
    @GetMapping("/form/getFormEntity")
    public AjaxResponse getFormEntity(HttpServletResponse response,
                                      @RequestParam("id") String id){
        FormEntity formEntity=formMapService.getFormEntity(id);
        if(formEntity!=null) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), formEntity);
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), "不存在id为["+id+"]的对象。");
        }
    }
    @PostMapping("/form/addForm")
    public AjaxResponse addForm(@RequestBody JSONObject param){
        JSONObject jsonObject=JSONObject.parseObject(param.getString("viewcode"));
        JSONObject configJson=JSONObject.parseObject(jsonObject.getString("config"));
        String msg="";
        String id= param.getString("formid");
        String name = configJson.getString("formRef");
        String createTime = DateTime.now().toString("yyyy-MM-dd hh:mm:ss");
        String formSort = "";//param.getString("formSort");
        String form_event = "";//param.getString("form_event");
        String form_event_link = "";//param.getString("form_event_link");
        String form_data_table = configJson.getString("tableName");
        String form_sub_tables = "";//param.getString("form_sub_tables");
        String form_json = param.getString("viewcode");

        FormEntity formEntity=formMapService.getFormEntity(id);
        if(formEntity==null) {
            msg = formMapService.addForm(id, name, createTime, formSort, form_event
                    , form_event_link, form_data_table, form_sub_tables, form_json);
        }
        else{
            msg=formMapService.updateForm(id,name,createTime,formSort,form_event
                    ,form_event_link,form_data_table,form_sub_tables,form_json);
        }

        if(msg.equals("执行成功")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "保存成功");
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
    @PostMapping("/form/updateForm")
    public AjaxResponse updateForm(@RequestBody JSONObject param){
        String id= UUID.randomUUID().toString();
        String name=param.getString("name");
        String createTime= DateTime.now().toString("yyyy-MM-dd hh:mm:ss");
        String formSort=param.getString("formSort");
        String form_event=param.getString("form_event");
        String form_event_link=param.getString("form_event_link");
        String form_data_table=param.getString("form_data_table");
        String form_sub_tables=param.getString("form_sub_tables");
        String form_json=param.getString("form_json");
        String msg=formMapService.updateForm(id,name,createTime,formSort,form_event
                ,form_event_link,form_data_table,form_sub_tables,form_json);
        if(msg.equals("执行成功")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "保存成功");
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
    @PostMapping("/form/deleteForm")
    public AjaxResponse deleteForm(@RequestBody JSONObject param){
        String msg=formMapService.deleteForm(param.getString("id"));
        if(msg.equals("执行成功")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), msg);
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
    @GetMapping("/form/formSortList")
    public AjaxResponse formSortList(){
        List<FormSortEntity> formSortList=formMapService.formSortList();
        if(formSortList.size()<=0){
            List<HashMap<String,Object>> mapList=new ArrayList<>();
            HashMap<String,Object> hashMap=new HashMap<>();
            hashMap.put("id","0");
            hashMap.put("name","请添加分类目录");
            hashMap.put("parentNo","0");
            hashMap.put("children",new ArrayList<>());
            mapList.add(hashMap);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), mapList);
        }
        else {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), formSortList);
        }
    }
    @GetMapping("/form/getFormSort")
    public AjaxResponse getFormSort(HttpServletResponse response,
                                    @RequestParam("id") String id){
        FormSortEntity formSortEntity=formMapService.getFormSort(id);
        if(formSortEntity!=null) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), formSortEntity);
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), "不存在id为["+id+"]的对象。");
        }
    }
    @PostMapping("/form/addFormSort")
    public AjaxResponse addFormSort(@RequestBody JSONObject param){
        String id=UUID.randomUUID().toString();
        String name=param.getString("name");
        String parentNo=param.getString("parentNo");
        String msg=formMapService.addFormSort(id,name,parentNo);
        if(msg.equals("执行成功")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), msg);
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
    @PostMapping("/form/deleteFormSort")
    public AjaxResponse deleteFormSort(@RequestBody JSONObject param){
        String id=param.getString("id");
        String msg=formMapService.deleteFormSort(id);
        if(msg.equals("执行成功")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), msg);
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
    @PostMapping("/form/updateFormSort")
    public AjaxResponse updateFormSort(@RequestBody JSONObject param){
        String id=UUID.randomUUID().toString();
        String name=param.getString("name");
        String parentNo=param.getString("parentNo");
        String msg=formMapService.updateFormSort(id,name,parentNo);
        if(msg.equals("执行成功")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), msg);
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
    @PostMapping("/form/saveFormDataJson")
    public AjaxResponse saveFormDataJson(@RequestBody JSONObject param){
        String id=param.getString("id");
        String mapJson=param.getString("mapJson");
        String dataJson=param.getString("dataJson");
        String msg=formMapService.saveFormDataJson(id,mapJson,dataJson);
        if(msg=="error"){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), msg);
        }
    }
//    @PostMapping("/form/getFromDataList")
//    public AjaxResponse getFromDataList(@RequestBody JSONObject param){
//        try{
//            int pageNum=param.getInteger("pageNum");
//            int pagesize=param.getInteger("pageSize");
//            int startIndex = (pageNum - 1) * pagesize;
//            String xmmc=param.getString("xmmc");
//            HashMap<String,Object> generWorkList=formMapService.getFromDataList(startIndex,pagesize,xmmc);
//            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
//                    SystemConfig.ResponseCode.SUCCESS.getDesc(), generWorkList);
//        }
//        catch (Exception ex){
//            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
//                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
//        }
//    }
    @PostMapping("/form/getTodoList")
    public AjaxResponse getTodoList(@RequestBody JSONObject param){
        try{
            int pageNum=param.getInteger("pageNum");
            int pagesize=param.getInteger("pageSize");
            int startIndex = (pageNum - 1) * pagesize;
            String flowName=param.getString("flowName");
            HashMap<String,Object> generWorkList=formMapService.getTodoList(startIndex,pagesize,flowName);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), generWorkList);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
        }
    }
    @GetMapping("/form/getGenerWork")
    public AjaxResponse getGenerWork(HttpServletResponse response,
                                        @RequestParam("id") String id){
        try{
            GenerWork generWork=formMapService.getGenerWork(id);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), generWork);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
        }
    }
    @GetMapping("/form/deleteGenerWork")
    public AjaxResponse deleteGenerWork(HttpServletResponse response,
                                        @RequestParam("id") String id){
        String msg=formMapService.deleteGenerWork(id);
        if(msg.equals("suess")){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功。");
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
}
